package com.wd.maven.aggregation;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AggregationProjectDialog extends DialogWrapper {

    private JBTextField groupIdField;
    private JBTextField artifactIdField;
    private JBTextField versionField;
    private JBTextField modulesField;
    private JComboBox<String> javaVersionComboBox;
    
    // 预定义模块复选框
    private JBCheckBox apiCheckBox;
    private JBCheckBox commonCheckBox;
    private JBCheckBox mapperCheckBox;
    private JBCheckBox pojoCheckBox;
    private JBCheckBox serviceCheckBox;

    public AggregationProjectDialog(Project project) {
        super(project);
        setTitle("创建Maven聚合项目");
        init();
    }

    @Override
    protected JComponent createCenterPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(5, 5, 5, 5);

        // Group ID
        c.gridx = 0;
        c.gridy = 0;
        panel.add(new JBLabel("Group ID:"), c);

        c.gridx = 1;
        c.weightx = 1.0;
        groupIdField = new JBTextField("com.example");
        panel.add(groupIdField, c);

        // Artifact ID
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 0.0;
        panel.add(new JBLabel("Artifact ID:"), c);

        c.gridx = 1;
        c.weightx = 1.0;
        artifactIdField = new JBTextField("demo");
        panel.add(artifactIdField, c);

        // Version
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 0.0;
        panel.add(new JBLabel("Version:"), c);

        c.gridx = 1;
        c.weightx = 1.0;
        versionField = new JBTextField("1.0-SNAPSHOT");
        panel.add(versionField, c);

        // Java Version
        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 0.0;
        panel.add(new JBLabel("Java Version:"), c);

        c.gridx = 1;
        c.weightx = 1.0;
        javaVersionComboBox = new JComboBox<>(new String[]{"8", "11", "17", "21"});
        panel.add(javaVersionComboBox, c);

        // 预定义模块
        c.gridx = 0;
        c.gridy = 4;
        c.weightx = 0.0;
        panel.add(new JBLabel("预定义模块:"), c);

        // 模块复选框面板
        JPanel modulesPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        apiCheckBox = new JBCheckBox("api", true);
        commonCheckBox = new JBCheckBox("common", true);
        mapperCheckBox = new JBCheckBox("mapper", true);
        pojoCheckBox = new JBCheckBox("pojo", true);
        serviceCheckBox = new JBCheckBox("service", true);
        
        modulesPanel.add(apiCheckBox);
        modulesPanel.add(commonCheckBox);
        modulesPanel.add(mapperCheckBox);
        modulesPanel.add(pojoCheckBox);
        modulesPanel.add(serviceCheckBox);

        c.gridx = 1;
        c.weightx = 1.0;
        panel.add(modulesPanel, c);

        // 自定义模块
        c.gridx = 0;
        c.gridy = 5;
        c.weightx = 0.0;
        panel.add(new JBLabel("自定义模块 (逗号分隔):"), c);

        c.gridx = 1;
        c.weightx = 1.0;
        modulesField = new JBTextField();
        panel.add(modulesField, c);

        return panel;
    }

    @Override
    protected ValidationInfo doValidate() {
        if (groupIdField.getText().trim().isEmpty()) {
            return new ValidationInfo("Group ID不能为空", groupIdField);
        }
        if (artifactIdField.getText().trim().isEmpty()) {
            return new ValidationInfo("Artifact ID不能为空", artifactIdField);
        }
        if (versionField.getText().trim().isEmpty()) {
            return new ValidationInfo("Version不能为空", versionField);
        }
        return null;
    }

    public String getGroupId() {
        return groupIdField.getText().trim();
    }

    public String getArtifactId() {
        return artifactIdField.getText().trim();
    }

    public String getVersion() {
        return versionField.getText().trim();
    }

    public String getJavaVersion() {
        return (String) javaVersionComboBox.getSelectedItem();
    }

    public String[] getModules() {
        List<String> modulesList = new ArrayList<>();
        
        // 添加选中的预定义模块
        if (apiCheckBox.isSelected()) modulesList.add("api");
        if (commonCheckBox.isSelected()) modulesList.add("common");
        if (mapperCheckBox.isSelected()) modulesList.add("mapper");
        if (pojoCheckBox.isSelected()) modulesList.add("pojo");
        if (serviceCheckBox.isSelected()) modulesList.add("service");
        
        // 添加自定义模块
        String customModules = modulesField.getText().trim();
        if (!customModules.isEmpty()) {
            modulesList.addAll(Arrays.asList(customModules.split("\\s*,\\s*")));
        }
        
        return modulesList.toArray(new String[0]);
    }
}